﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Cloud.Framework.Algorithm
{
    public class KeyNodeValue
    {
        private static ArrayList _ArrayList = new ArrayList();
        private static ReaderWriterLock sReaderWriterLock = new ReaderWriterLock();
        /// <summary>
        /// 算法
        /// </summary>
        /// <param name="iData"></param>
        /// <returns></returns>
        private static Int64 ConvertNO(string iData)
        {
            //累加计算
            Int64 _Count = 0;
            //转码
            byte[] byteE = System.Text.Encoding.UTF8.GetBytes(iData);
            //计算
            for (int i = 0; i < byteE.Length; i++)
            {
                _Count += byteE[i] + (byteE[i] * byteE[i] * byteE.Length);
            }
            //输出
            return _Count;
        }
        /// <summary>
        /// 添加ip地址
        /// </summary>
        /// <param name="iIP"></param>
        /// <returns></returns>
        public static bool Add(object iIP)
        {
            sReaderWriterLock.AcquireWriterLock(100);
            try
            {
                if (_ArrayList.Add(iIP) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            finally
            {
                sReaderWriterLock.ReleaseWriterLock();
            }
        }
        /// <summary>
        /// 删除一个IP地址
        /// </summary>
        /// <param name="iIndex">数组下标（服务器编号）</param>
        /// <returns>true 提示:（try catch处理异常）</returns>
        public static bool Delect(int iIndex)
        {
            sReaderWriterLock.AcquireWriterLock(100);
            try
            {
                _ArrayList.RemoveAt(iIndex);
            }
            finally
            {
                sReaderWriterLock.ReleaseWriterLock();
            }
            return true;
        }
        /// <summary>
        /// 修改一个IP地址
        /// </summary>
        /// <param name="iIndex"></param>
        /// <param name="iIP"></param>
        /// <returns></returns>
        public static bool Update(int iIndex, object iIP)
        {
            sReaderWriterLock.AcquireWriterLock(100);
            try
            {
                _ArrayList[iIndex] = iIP;
            }
            finally
            {
                sReaderWriterLock.ReleaseWriterLock();
            }
            return true;
        }
        /// <summary>
        /// 修改一个IP地址
        /// </summary>
        /// <param name="iIndex"></param>
        /// <param name="iIP"></param>
        /// <returns></returns>
        public static bool Update(string iIndex, object iIP)
        {
            Update(Convert.ToInt32(iIndex), iIP);
            return true;
        }
        /// <summary>
        /// 总数
        /// </summary>
        /// <returns>int</returns>
        public static int Count()
        {
            int count = 0;
            sReaderWriterLock.AcquireReaderLock(100);
            try
            {
                count = _ArrayList.Count;
            }
            finally
            {
                sReaderWriterLock.ReleaseReaderLock();
            }
            return count;
        }
        /// <summary>
        /// 删除所有ip地址
        /// </summary>
        /// <returns>true 提示:（try catch处理异常）</returns>
        public static bool Clear()
        {
            sReaderWriterLock.AcquireWriterLock(100);
            try
            {
                _ArrayList.Clear();
            }
            finally
            {
                sReaderWriterLock.ReleaseWriterLock();
            }
            return true;
        }
        /// <summary>
        /// 使用下标获取地址
        /// </summary>
        /// <param name="iIndex"></param>
        /// <returns></returns>
        public static object Get(int iIndex)
        {
            object objectc = null;
            sReaderWriterLock.AcquireReaderLock(100);
            try
            {
                objectc = _ArrayList[iIndex];
            }
            finally
            {
                sReaderWriterLock.ReleaseReaderLock();
            }
            return objectc;
        }
        /// <summary>
        /// 使用下标获取地址
        /// </summary>
        /// <param name="iIndex"></param>
        /// <returns></returns>
        public static object Get(string iIndex)
        {
            return Get(Convert.ToInt32(iIndex));
        }
        /// <summary>
        /// 通过Key获取 NodeIP
        /// </summary>
        /// <param name="iKey"></param>
        /// <returns>string null没有数据</returns>
        public static object GetNode(string iKey)
        {
            object objectc = null;
            sReaderWriterLock.AcquireReaderLock(100);
            try
            {
                if (Count() != 0)
                {
                    int i = (int)ConvertNO(iKey) % Count();
                    objectc = _ArrayList[i];
                }
            }
            finally
            {
                sReaderWriterLock.ReleaseReaderLock();
            }
            return objectc;
        }
        /// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="i">下标</param>
        /// <param name="iIP">参数</param>
        /// <returns></returns>
        public static bool SetNode(int i, object iIP)
        {
            sReaderWriterLock.AcquireWriterLock(100);
            try
            {
                _ArrayList[i] = iIP;
            }
            finally
            {
                sReaderWriterLock.ReleaseWriterLock();
            }
            return true;
        }
        /// <summary>
        /// 写入数据
        /// </summary>
        /// <param name="i">下标</param>
        /// <param name="iIP">参数</param>
        /// <returns></returns>
        public static bool SetNode(string i, object iIP)
        {
            SetNode(Convert.ToInt32(i), iIP);
            return true;
        }
    }
}
